package com.ruoyi.order.controller;

import com.alipay.api.AlipayApiException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.domain.PayData;
import com.ruoyi.order.domain.VehicleOrder;
import com.ruoyi.order.service.IVehicleOrderService;
import com.ruoyi.order.util.ZhifubaoUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/aliPay")
public class AlipayController {
    @Autowired
    private IVehicleOrderService vehicleOrderService;

    @Autowired
    private ZhifubaoUtill zhifubaoUtill;

    @GetMapping("/getOrderNum/{id}")
    public String getOrderNum(@PathVariable Long id) throws AlipayApiException {
        //根据订单id查询订单
        VehicleOrder vehicleOrder = vehicleOrderService.getOrderById(id);
        PayData payData = new PayData();
        payData.setCode(vehicleOrder.getOrderNo());
        payData.setName("网约车订单支付");
        payData.setMoney(vehicleOrder.getFare());

        String pay = zhifubaoUtill.pay(payData);
        return pay;
    }

    @PostMapping("payOKResult")
    public String payOKResult(@RequestParam String out_trade_no) throws AlipayApiException {
        Boolean aBoolean = zhifubaoUtill.queryResult(out_trade_no);

        VehicleOrder vehicleOrder = new VehicleOrder();
        vehicleOrder.setOrderNo(out_trade_no);

        if(aBoolean){
            vehicleOrder.setOrderStatus("1");
            vehicleOrderService.updateOrderStatus(vehicleOrder);
            return "支付成功";
        }
        vehicleOrder.setOrderStatus("2");
        vehicleOrderService.updateOrderStatus(vehicleOrder);
        return "支付失败";
    }
}
